package com.jiutong.meipengdai.ui.activitys;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jiutong.meipengdai.R;
import com.jiutong.meipengdai.base.BaseActivity;
import com.jiutong.meipengdai.base.CommonAdapter;
import com.jiutong.meipengdai.base.ViewHolder;
import com.jiutong.meipengdai.entity.MyloanBean;
import com.jiutong.meipengdai.http.model.interfaces.CheckLoginInterface;
import com.jiutong.meipengdai.http.model.interfaces.LoanRelaBize;
import com.jiutong.meipengdai.http.present.LoanRelaPresent;
import com.jiutong.meipengdai.utils.NumUilt;
import com.jiutong.meipengdai.views.dialog.BaseDialog;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 所有貸款
 */

@CheckLoginInterface
public class AllLoanActivity extends BaseActivity implements LoanRelaBize {

    @Bind(R.id.tv_loan_all)
    TextView tvLoanAll;
    @Bind(R.id.tv_loan_returning)
    TextView tvLoanReturning;
    @Bind(R.id.tv_loan_compelete)
    TextView tvLoanCompelete;
    @Bind(R.id.line1)
    View line1;
    @Bind(R.id.line2)
    View line2;
    @Bind(R.id.line3)
    View line3;
    @Bind(R.id.iv_back)
    ImageView iv_back;
    @Bind(R.id.rl_back)
    RelativeLayout rl_back;
    @Bind(R.id.tv_no_date)
    TextView tvNoDate;

    private LoanRelaPresent loanRelaPresent;

    private ListView loan_list;

    private TextView vt_title;
    private List<MyloanBean.DataBean> returning = new ArrayList<>();
    private List<MyloanBean.DataBean> overloan = new ArrayList<>();
    private List<MyloanBean.DataBean> data;
    private List<MyloanBean.DataBean> recdList = new ArrayList<>();
    private BaseDialog dialog;


    @Override
    public int getLayoutResouceId() {
        return R.layout.activity_all_loan;
    }

    @Override
    protected void initDate(Bundle savedInstanceState) {
        super.initDate(savedInstanceState);
        loanRelaPresent = new LoanRelaPresent(mContext, this);
        loanRelaPresent.loanRecord("");
    }

    @Override
    protected void initView() {
        ButterKnife.bind(this);
        loan_list = (ListView) findViewById(R.id.loan_item_List);
        vt_title = (TextView) findViewById(R.id.tv_title);

        vt_title.setText("我的贷款");
    }

    @Override
    protected void initEvent() {
        super.initEvent();
    }


    @Override
    public String getAreaName() {
        return null;
    }

    @Override
    public String getLocaName() {
        return null;
    }

    @Override
    public String getOrientate() {
        return null;
    }

    @Override
    public String getArea() {
        return null;
    }

    @Override
    public String getInFloor() {
        return null;
    }

    @Override
    public String getTotalFloor() {
        return null;
    }

    @Override
    public String getHouseNum() {
        return null;
    }

    @Override
    public void onSuccess(Object object) {
        returning.clear();
        overloan.clear();
        MyloanBean myloanBean = (MyloanBean) object;
        data = myloanBean.getData();
        if (data.size() <= 0) {
            showBaseDialog();
            return;
        }

        tvNoDate.setVisibility(View.GONE);

        for (int i = 0; i < data.size(); i++) {
            String loanStatus = data.get(i).getLoanStatus();
            if (!loanStatus.equals("0")) {
                recdList.add(data.get(i));
            }
        }

        LoanRecordsAdapter loanRecordsAdapter = new LoanRecordsAdapter(mContext, recdList, R.layout.loan_item_list);
        loan_list.setAdapter(loanRecordsAdapter);
        for (int i = 0; i < data.size(); i++) {
            String loanStatus = data.get(i).getLoanStatus();
            if (loanStatus.equals("5")) {
                returning.add(data.get(i));
            } else if (loanStatus.equals("6")) {
                overloan.add(data.get(i));
            }
        }
    }

    private void showBaseDialog() {
        dialog = new BaseDialog.Builder()
                .layResuorce(R.layout.pop_loan_apply)
                .animResource(R.style.dialog_animation_fade)
                .okViewClickEvent(R.id.btn_submit)
                .cancelViewClickEvent(R.id.iv_cancel)
                .gravityStyle(Gravity.CENTER)
                .outSideCancel(true)
                .dialogWidth(-2)
                .build();

        dialog.setOnDialogClickListener(new BaseDialog.OnDialogClickListener() {
            @Override
            public void onOkClick() {
                dialog.dismiss();
                openActivity(EvaluateActivity.class);
                finish();
            }

            @Override
            public void onOk2Click() {

            }
        });
        dialog.show(getSupportFragmentManager(), "dialog");
    }

    @Override
    public void onError(Throwable e) {

    }


    @OnClick({R.id.tv_loan_all, R.id.tv_loan_returning, R.id.tv_loan_compelete, R.id.rl_back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_loan_all:
                line1.setVisibility(View.VISIBLE);
                line2.setVisibility(View.INVISIBLE);
                line3.setVisibility(View.INVISIBLE);
                tvLoanAll.setTextColor(getResources().getColor(R.color.theme_orange));
                tvLoanReturning.setTextColor(getResources().getColor(R.color.gray));
                tvLoanCompelete.setTextColor(getResources().getColor(R.color.gray));

                if(recdList.size() < 1){
                    tvNoDate.setVisibility(View.VISIBLE);
                    return;
                }
                tvNoDate.setVisibility(View.GONE);
                LoanRecordsAdapter loanRecordsAdapter1 = new LoanRecordsAdapter(mContext, recdList, R.layout.loan_item_list);
                loan_list.setAdapter(loanRecordsAdapter1);
                loanRecordsAdapter1.notifyDataSetChanged();
                break;
            case R.id.tv_loan_returning:
                line1.setVisibility(View.INVISIBLE);
                line2.setVisibility(View.VISIBLE);
                line3.setVisibility(View.INVISIBLE);
                tvLoanAll.setTextColor(getResources().getColor(R.color.gray));
                tvLoanReturning.setTextColor(getResources().getColor(R.color.theme_orange));
                tvLoanCompelete.setTextColor(getResources().getColor(R.color.gray));

                if(returning.size() < 1){
                    tvNoDate.setVisibility(View.VISIBLE);
                    return;
                }
                tvNoDate.setVisibility(View.GONE);
                LoanRecordsAdapter loanRecordsAdapter2 = new LoanRecordsAdapter(mContext, returning, R.layout.loan_item_list);
                loan_list.setAdapter(loanRecordsAdapter2);
                loanRecordsAdapter2.notifyDataSetChanged();
                break;
            case R.id.tv_loan_compelete:
                line1.setVisibility(View.INVISIBLE);
                line2.setVisibility(View.INVISIBLE);
                line3.setVisibility(View.VISIBLE);
                tvLoanAll.setTextColor(getResources().getColor(R.color.gray));
                tvLoanReturning.setTextColor(getResources().getColor(R.color.gray));
                tvLoanCompelete.setTextColor(getResources().getColor(R.color.theme_orange));

                if(overloan.size() < 1){
                    tvNoDate.setVisibility(View.VISIBLE);
                    return;
                }
                tvNoDate.setVisibility(View.GONE);
                LoanRecordsAdapter loanRecordsAdapter3 = new LoanRecordsAdapter(mContext, overloan, R.layout.loan_item_list);
                loan_list.setAdapter(loanRecordsAdapter3);
                loanRecordsAdapter3.notifyDataSetChanged();
                break;
            case R.id.rl_back:
                finish();
                break;
        }
    }

    public class LoanRecordsAdapter extends CommonAdapter<MyloanBean.DataBean> {

        public LoanRecordsAdapter(Context context, List<MyloanBean.DataBean> listDatas, int layoutId) {
            super(context, listDatas, layoutId);
        }

        @Override
        protected void fillData(ViewHolder holder, final int position) {
            TextView tv_price = holder.getView(R.id.tv_price);
            TextView tvUnit = holder.getView(R.id.tv_unit);
            TextView tv_apply_time = holder.getView(R.id.tv_apply_time);
            TextView tv_type = holder.getView(R.id.tv_type);
            View loan_state = holder.getView(R.id.loan_state);
            TextView tv_total_period = holder.getView(R.id.tv_total_period);
            TextView tvHouseId = holder.getView(R.id.tv_house_id);
            TextView tv_type_show_ex = holder.getView(R.id.tv_type_show_ex);
            LinearLayout ll_loan_record_item = holder.getView(R.id.ll_loan_record_item);
            tv_total_period.setText("共" + listDatas.get(position).getLoanDeadline() + "期");
            MyloanBean.DataBean dataBean = listDatas.get(position);
            double amont = Double.parseDouble(dataBean.getLoanAmount());
            if (amont >= 10000) {
                tv_price.setText(NumUilt.formatDouble(amont / 10000));
                tvUnit.setText("万");
            } else {
                tvUnit.setText("元");
                tv_price.setText(NumUilt.formatDouble(amont));
            }
            tv_apply_time.setText(listDatas.get(position).getApplyDate() + " 贷");
            tvHouseId.setText(listDatas.get(position).getHouseCardFull());
            String loanStatus = listDatas.get(position).getLoanStatus();
            ll_loan_record_item.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Bundle bundle = new Bundle();
                    bundle.putString("loanId", listDatas.get(position).getLoanId());
                    bundle.putParcelable("date", listDatas.get(position));
                    openActivity(LoanDetailActivity.class, bundle);
                }
            });
            if (!TextUtils.isEmpty(loanStatus)) {
                int statusNum = Integer.parseInt(loanStatus);
                if (statusNum == 1 || statusNum == 2 || statusNum == 3) {
                    tv_type.setText("审核中");
                    loan_state.setBackgroundResource(R.color.green);
                    tv_type.setTextColor(getResources().getColor(R.color.green));
                }
                if (statusNum == 5) {
                    tv_type.setText("还款中");
                    loan_state.setBackgroundResource(R.color.orange);
                    tv_type.setTextColor(getResources().getColor(R.color.orange));
                }
                if (statusNum == 6) {
                    loan_state.setBackgroundResource(R.color.gray);
                    tv_type.setTextColor(getResources().getColor(R.color.gray));
                    tv_type.setText("已还清");
                }

                if (statusNum == 4) {
                    loan_state.setBackgroundResource(R.color.red);
                    tv_type.setTextColor(Color.RED);
                    tv_type.setText("未通过");
                }
            }

            String showExtension = listDatas.get(position).getShowExtension();
            if (!TextUtils.isEmpty(showExtension)) {
                if ("0".equals(showExtension)) {
                    tv_type_show_ex.setVisibility(View.VISIBLE);
                    tv_type_show_ex.setText("可申请展期");
                } else if ("1".equals(showExtension)) {
                    tv_type_show_ex.setVisibility(View.VISIBLE);
                    tv_type_show_ex.setText("展期审核中");
                }else if ("2".equals(showExtension)) {
                    tv_type_show_ex.setVisibility(View.VISIBLE);
                    tv_type_show_ex.setText("展期审核失败");
                }else if ("3".equals(showExtension)) {
                    tv_type_show_ex.setVisibility(View.VISIBLE);
                    tv_type_show_ex.setText("展期申请成功");
                }else if ("-1".equals(showExtension)) {
//                    tv_type_show_ex.setVisibility(View.VISIBLE);
//                    tv_type_show_ex.setText("不可申请展期");v
                }
            }
        }
    }
}
